package com.betterjr.modules.risk.api.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.betterjr.common.utils.Collections3;
import com.betterjr.common.web.AbstarctRemoteApiInvoke;
import com.betterjr.common.web.AjaxObject;
import com.betterjr.common.web.AjaxRemoteApiParam;
import com.betterjr.modules.risk.loan.service.LoanProjectRelationService;

@Service
public class RemoteApiQueryLoanProjectInvokeService extends AbstarctRemoteApiInvoke {

    @Autowired
    private LoanProjectRelationService relationService;

    private String apiCode = "RAI00011";

    @Override
    public String saveOrFindInvoke(AjaxRemoteApiParam param) {

        return AjaxObject.newOkWithPage(" 查询成功",
                relationService.queryLoanRelations(convertToUseMap(param.getParamMap()),
                        findProperties(param.getParamMap(), "orderBy", " id Desc "),
                        findProperties(param.getParamMap(), "flag", "0"),
                        findProperties(param.getParamMap(), "pageNum", Integer.class, 1),
                        findProperties(param.getParamMap(), "pageSize", Integer.class, 10)))
                .toJson();
    }

    @Override
    public boolean supportInvoke(String anApiCode) {

        return apiCode.equals(anApiCode);
    }

    @Override
    public boolean checkParam(Map<String, Object> anParamMap) {

        boolean flag = true;

        if (flag && Collections3.isEmpty(anParamMap)) flag = false;

        /* if (flag && !anParamMap.containsKey("businStatus")) flag = false;
        
        boolean isCoreNoCustCode = anParamMap.containsKey("custCode") && anParamMap.containsKey("coreNo");
        
        boolean isFactoryNo = anParamMap.containsKey("factoryNo");
        
        if (flag && !(isCoreNoCustCode || isFactoryNo)) flag = false;*/

        return flag;

    }

    private Map<String, Object> convertToUseMap(Map<String, Object> sourceMap) {

        Map<String, Object> map = new HashMap<>(sourceMap.size());

        map.put("businStatus", sourceMap.get("businStatus"));

        if (sourceMap.containsKey("custCode")) {

            map.put("custCode", sourceMap.get("sourceMap"));
        }

        if (sourceMap.containsKey("coreNo")) {

            map.put("coreNo", findProperties(sourceMap, "coreNo", Long.class));
        }

        if (sourceMap.containsKey("factoryNo")) {

            map.put("factoryNo", findProperties(sourceMap, "factoryNo", Long.class));
        }

        return map;

    }

}
